package com.garen.fire.biz.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.garen.fire.biz.IJPushBiz;
import com.garen.fire.dao.IFireCaseInfoDao;
import com.garen.fire.entity.FireCaseInfo;
import com.garen.sys.dao.IFireOrgDao;
import com.garen.sys.entity.FireOrg;

@Service
public class JPushBIzImpl implements IJPushBiz {

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private IFireOrgDao fireOrgDao;
	
	@Autowired
	private IFireCaseInfoDao fireCaseInfoDao;
	

//	private static final String APP_KEY = "ebb192814bacf185f317504c";
//	private static final String MASTER_SECRET = "d3d3848752b2ba62239b0be0";
//	
	private static final String AUTHORIZATION = "Basic ZWJiMTkyODE0YmFjZjE4NWYzMTc1MDRjOmQzZDM4NDg3NTJiMmJhNjIyMzliMGJlMA==";
	
	private static final String JPUSH_URL = "https://api.jpush.cn/v3/push";

	@Override
	public void pushMessage(int pushType, FireCaseInfo caseInfo) {
		// TODO Auto-generated method stub
		

		String ajbh = caseInfo.getAjbh();
		String zhdz = caseInfo.getZhdz();
		
		FireOrg org = fireOrgDao.getFireOrgByAjbh(ajbh).get(0);
		List<FireOrg> parentOrgList = fireOrgDao.getAllParentOrgByOrgId(org.getId());
		
		String alert = "通知";
		
		
		List<Object> tagAnd = new ArrayList<Object>();
		List<Object> tag= new ArrayList<Object>();
		
		tag.add("org_"+org.getId());
		if(parentOrgList!=null)
		{
			for (FireOrg o : parentOrgList) {
				tag.add("org_"+o.getId());
			}
		}
		
		switch (pushType) {
			case JPUSH_TYPE_NEW_FIGHT:	//新案情通知案情所属单位及其所有上级单位，不区分人员类型
				alert = "发生新案情:"+zhdz+"。";
				break;
			case JPUSH_TYPE_SET_OUT://案情更新通知案情所属单位及所有上级单位的领导
				alert = org.getOrgShortName() + "已出发，赶往案情\""+zhdz+"\"现场。";
				tagAnd.add(JPUST_TAG_LEADER);
				break;
			case JPUSH_TYPE_ARRIVE://案情更新通知案情所属单位及所有上级单位的领导
				alert = org.getOrgShortName() + "已到达案情\""+zhdz+"\"现场。";
				tagAnd.add(JPUST_TAG_LEADER);
				break;
			case JPUSH_TYPE_COMPLETE:	//案情更新通知案情所属单位及所有上级单位的领导
				alert = "案情\""+zhdz+"\"已处置完毕。";
				tagAnd.add(JPUST_TAG_LEADER);
				break;

		}
		


		JSONObject request = new JPushRequestBuilder()
				.setPlatform(JPUSH_PLATFORM_ALL)
				.setNotificationAlert(alert)
				.setAudienceTagAnd(tagAnd)
				.setAudienceTag(tag)
				.putExtra("ajbh", ajbh)
				.putExtra("type", pushType+"")
				.build();

		pushMessage(request);

	}

	private void pushMessage(JSONObject request) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Authorization", AUTHORIZATION);
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");  
        headers.setContentType(type);  
		
		String requestString = request.toString();
		HttpEntity<String> entity = new HttpEntity<String>(requestString, headers);
		restTemplate.postForObject(JPUSH_URL, entity, String.class);

	}



	private class JPushRequestBuilder {

		private JSONObject request;
		private JSONObject audience;
		private JSONObject notification;
//		private JSONObject message;
		private JSONObject extras;

		public JPushRequestBuilder() {
			request = new JSONObject();
			audience = new JSONObject();
			notification = new JSONObject();
//			message = new JSONObject();
			extras = new JSONObject();
		}

		public JPushRequestBuilder setPlatform(String platform) {
			this.request.put("platform", platform);
			return this;
		}

//		public JPushRequestBuilder setMessageContent(String messageContent) {
//			this.message.put("message_content", messageContent);
//			return this;
//		}
//
//		public JPushRequestBuilder setMessageTitle(String title) {
//			this.message.put("title", title);
//			return this;
//		}
//
//		public JPushRequestBuilder setMessageContentType(String contentType) {
//			this.message.put("content_type", contentType);
//			return this;
//		}

		public JPushRequestBuilder setNotificationAlert(String alert) {
			this.notification.put("alert", alert);
			return this;
		}

		public JPushRequestBuilder setAudienceTag(List<Object> tag) {
			if(tag==null||tag.size()<=0)
			{
				return this;
			}
			JSONArray array = new JSONArray(tag);
			this.audience.put("tag", array);
			return this;
		}

		public JPushRequestBuilder setAudienceTagAnd(List<Object> tag) {
			if(tag==null||tag.size()<=0)
			{
				return this;
			}
			JSONArray array = new JSONArray(tag);
			this.audience.put("tag_and", array);
			return this;
		}

		public JPushRequestBuilder setAudienceTagNot(List<Object> tag) {
			if(tag==null||tag.size()<=0)
			{
				return this;
			}
			JSONArray array = new JSONArray(tag);
			this.audience.put("tag_not", array);
			return this;
		}
		public JPushRequestBuilder putExtra(String key,String value) {
			this.extras.put(key, value);
			return this;
		}
		
		

		public JSONObject build() {

			JSONObject android = new JSONObject();
			android.put("extras", this.extras);
			
			this.notification.put("android", android);
			this.request.put("audience", this.audience);
			this.request.put("notification", this.notification);
//			this.request.put("message", this.message);
			
			return this.request;
		}

	}



	@Override
	public void pushMessage(int pushType, String ajbh) {
		// TODO Auto-generated method stub
		FireCaseInfo caseInfo  = this.fireCaseInfoDao.getFireCaseInfoByAjbh(ajbh);
		if(caseInfo!=null)
		{
			pushMessage(pushType, caseInfo);
		}
	}

}
